package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ResourceGroupRouter struct {
	resourceGroupService service.ResourceGroupService
}

func NewResourceGroupRouter() ResourceGroupRouter {
	return ResourceGroupRouter{resourceGroupService: service.GetResourceGroupService()}
}

func (ResourceGroupRouter ResourceGroupRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/resource/group/list", ResourceGroupRouter.QueryResourceGroup())
	r.POST(apiVersion+"/resource/group/create", ResourceGroupRouter.createResourceGroup())
	r.POST(apiVersion+"/resource/group/delete", ResourceGroupRouter.deleteResourceGroup())
}

func (ResourceGroupRouter ResourceGroupRouter) QueryResourceGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceGroupQueryDTO dto.ResourceGroupQueryDTO
		err := context.ShouldBind(&resourceGroupQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceGroupRouter.resourceGroupService.QueryResourceGroup(resourceGroupQueryDTO))
	}
}

func (ResourceGroupRouter ResourceGroupRouter) createResourceGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceGroupBaseDTO dto.ResourceGroupBaseDTO
		err := context.ShouldBind(&resourceGroupBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceGroupRouter.resourceGroupService.CreateResourceGroup(resourceGroupBaseDTO))
	}
}

func (ResourceGroupRouter ResourceGroupRouter) deleteResourceGroup() gin.HandlerFunc {
	return func(context *gin.Context) {
		var resourceGroupDelDTO dto.ResourceGroupDelDTO
		err := context.ShouldBind(&resourceGroupDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, ResourceGroupRouter.resourceGroupService.DelResourceGroup(resourceGroupDelDTO))
	}
}
